package com.jams.goodox.activity.home;

import android.content.Intent;
import android.util.Base64;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.hangzhou.jin.customview.TextMoreLayout;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.mine.address.MineAddressActivity;
import com.jams.goodox.entity.Address;
import com.jams.goodox.entity.GoodBuyBean;
import com.jams.goodox.entity.MyServiceAddress;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HomeService;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.NoScrollGridView;
import com.jams.goodox.widget.NoScrollListView;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;
import com.jams.library.fresco.FrescoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/7.
 */

public class PayGoodActivity extends BaseActivity {
    @Bind(R.id.tx_userName)
    TextView txUserName;
    @Bind(R.id.tx_userTel)
    TextView txUserTel;
    @Bind(R.id.tx_address_icon)
    TextView txAddressIcon;
    @Bind(R.id.tx_address)
    TextView txAddress;
    @Bind(R.id.linear_address)
    LinearLayout linearAddress;
    @Bind(R.id.scroll_list)
    NoScrollListView scrollList;
    @Bind(R.id.tx_pay_style)
    TextMoreLayout txPayStyle;
    @Bind(R.id.tx_yunshu)
    TextMoreLayout txYunshu;
    @Bind(R.id.tx_pay)
    TextView txPay;
    @Bind(R.id.tx_pay_money)
    TextView txPayMoney;
    @Bind(R.id.tx_introduce)
    TextMoreLayout tx_introduce;

    private Address address;
    private String logistic="德邦快递";
    private String introStr="";
    private ArrayList<GoodBuyBean.Data> list;

    @Override
    protected int getLayoutId() {
        return R.layout.pay_good_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "结算中心");
        setListViewAdapter();
        loadData();
    }

    private void setListViewAdapter() {
        list=(ArrayList<GoodBuyBean.Data>)getIntent().getSerializableExtra("beanList");
        scrollList.setAdapter(new ListCommonAdapter<GoodBuyBean.Data>(this, list, R.layout.order_fragment_list_item) {
            @Override
            public void convert(ListCommonViewHolder baseViewHolder, GoodBuyBean.Data item, int position) {
                baseViewHolder.setText(R.id.tx_price_number,String.format(" ¥ %s  *%s",item.getPrice(),item.getNum()));
                baseViewHolder.setText(R.id.tx_order_code,String.format("%s. %s",String.valueOf(position),               item.getTitle()));
                baseViewHolder.setText(R.id.tx_time,item.getTime());
                SimpleDraweeView simpleDraweeView=baseViewHolder.getView(R.id.sdv_product);
                FrescoUtil.loadNetUrl(simpleDraweeView,item.getPreview());

                NoScrollGridView grid_good_ms=baseViewHolder.getView(R.id.grid_good_ms);
                grid_good_ms.setFocusable(false);
                grid_good_ms.setAdapter(new ListCommonAdapter<GoodBuyBean.Data.OptionBean>(PayGoodActivity.this,item.getOption(),R.layout.shop_type_item) {
                    @Override
                    public void convert(ListCommonViewHolder helper, GoodBuyBean.Data.OptionBean item, int position) {
                        helper.setText(R.id.tx_type,String.format("%s/%s",item.getOptionKey(),item.getName()));
                    }
                });
            }
        });
        double allMoney=0;
        for (GoodBuyBean.Data data:list){
            allMoney+= StringUtils.toDouble(data.getPrice())*StringUtils.toDouble(data.getNum());
        }
        txPayMoney.setText(String.format("¥ %s",String.valueOf(allMoney)));
    }

    @OnClick({R.id.linear_address, R.id.tx_pay_style, R.id.tx_yunshu,R.id.tx_introduce, R.id.tx_pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_address:
                startActivityForResult(new Intent(this, MineAddressActivity.class),1001);
                break;
            case R.id.tx_pay_style:
//                startActivity(new Intent(this,ChoosePayWayActivity.class));
                ToastUtil.show("久邦仅支持押金支付");
                break;
            case R.id.tx_yunshu:
                startActivityForResult(new Intent(this,ChooseWuliuActivity.class),1002);
                break;
            case R.id.tx_introduce:
                break;
            case R.id.tx_pay:
                submitOrder();
                break;
        }
    }

    /**
     * 提交订单
     */
    private void submitOrder(){
        SubmitBean goodBuyBean=new SubmitBean();
        goodBuyBean.setAddress(address);
        goodBuyBean.setNote(introStr);
        goodBuyBean.setPayType("押金支付");
        goodBuyBean.setTransit(logistic);
        ArrayList<SubmitBean.SubmitDataBean> submitList=new ArrayList<>();
        for(GoodBuyBean.Data data:list){
            SubmitBean.SubmitDataBean submitDataBean=data.transForData();
            submitDataBean.setOptionJson(data.transFor());
            submitList.add(submitDataBean);
        }
        goodBuyBean.setProduct(submitList);
        String jsonStr=new Gson().toJson(goodBuyBean);
        Base64.encodeToString(jsonStr.getBytes(), Base64.DEFAULT);
        showLoading();
        enqueue(createCallService(HomeService.class).orderSubmit(SePref.getToken(),  Base64.encodeToString(jsonStr.getBytes(), Base64.DEFAULT)), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                misLoading();
                ToastUtil.show("提交成功");
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    /**
     * 获取地址
     */
    private void loadData() {
        showLoading();
        enqueue(createCallService(HomeService.class).addressList(SePref.getToken()), new RetrofitCallbackListener<MyServiceAddress>() {
            @Override
            public void onSuccess(Call<MyServiceAddress> call, MyServiceAddress result) {
                misLoading();
                List<Address> addressList = result.getData();
                if(addressList!=null&&addressList.size()>0){
                    address=addressList.get(0);
                    txUserName.setText(address.getName());
                    txUserTel.setText(address.getPhone());
                    txAddress.setText(String.format("%s\n%s",address.getCity(),address.getAddressDetail()));
                }

            }

            @Override
            public void onError(String msg) {
                misLoading();
                ToastUtil.show(msg);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode){
                case 1001:
                    if(data.getSerializableExtra("bean")!=null){
                        address=(Address) data.getSerializableExtra("bean");
                        txUserName.setText(address.getName());
                        txUserTel.setText(address.getPhone());
                        txAddress.setText(String.format("%s\n%s",address.getCity(),address.getAreaAddress()));
                    }
                    break;
                case 1002:
                    logistic=data.getStringExtra("logistic");
                    txYunshu.setRightText(logistic);
                    break;
                case 1003:
                    introStr=data.getStringExtra("introStr");
                    tx_introduce.setRightText(introStr);
                    break;
            }
        }
    }
}
